/**
* Created on 2017/8/15.
* @fileoverview 请填写简要的文件说明.
* @author joc (Chen Wen)
*/
const getUrl = function (version, key) {
return `http://webapi.amap.com/maps?v=${version}&key=${key}`;
};
const checkExists = function (el, err) {
if (!el) {
throw new Error(err);
}
};
const getElementById = function (id, options) {
options = options || {};
let parent = options.parent || document.body;
if (typeof parent === 'string') {
parent = getElementById(parent);
}
const searchIn = options.searchIn || parent;
const elType = options.elType || 'div';
if (!id) {
return null;
}
if (id instanceof Element && !id.parentElement) {
parent.appendChild(id);
return id;
}
let el = searchIn.querySelector(`#${id}`);
if (!el) {
el = document.createElement(elType);
el.id = id;
parent.appendChild(el);
}
return el;
};
const AMapSource = function (options) {
const self = this;
checkExists(options && typeof options === 'object', 'Expect an object argument: `options`.');
self.panelEl = getElementById(options.panelEl, {elType: options.panelType});
options = Object.assign({
version: '1.3',
scriptLoaded: false,
scriptPanel: self.panelEl,
mapOptions: {},
containerConfig: {},
scriptId: 'script'
}, options);
const version = options.version;
const key = options.key;
checkExists(key, 'Expect a `key` property in `options`.');
checkExists(options.panelEl, 'Expect a `panelEl` property in `options`.');
self.url = getUrl(version, key);
self.setLoaded(options.scriptLoaded);
self.version = version;
self.key = key;
self.mapOptions = options.mapOptions;
self._pendingCallbacks = [];
self.container = self._getContainer(options.containerConfig);
self._initCreate(options);
self.postCreated = options.postCreated;
self.lazy = !!options.lazy;
self.lazy || self.render();
};
AMapSource.prototype._getContainer = function (config) {
checkExists(config && typeof config === 'object', 'expect an object argument: `config`');
return getElementById(config.id || this.panelEl.id, Object.assign({
parent: this.panelEl
}, config));
};
AMapSource.prototype.setLoaded = function (flg) {
if (!this.scriptLoaded) {
this.scriptLoaded = flg;
}
};
AMapSource.prototype.loadScript = function () {
const self = this;
self.setLoaded(false);
const script = self.scriptEl = getElementById(self.scriptId, {elType: 'script', parent: self.scriptPanel});
return new Promise(function (resolve) {
const onload = function () {
resolve.apply(this, AMap);
self.setLoaded(true);
};
if (self.scriptLoaded) {
return onload();
}
script.src = self.url;
script.onload = onload;
});
};
AMapSource.prototype._create = function (AMap) {
return new AMap.Map(this.container.id, Object.assign({
resizeEnable: true,
zoom: 10
}, this.mapOptions));
};
AMapSource.prototype._initCreate = function (options) {
const self = this;
let create = options.create;
if (!create) {
create = self._create;
}
self.create = function () {
const r = create.call(self, AMap);
self.postCreated && self.postCreated(r);
return r;
};
};
AMapSource.prototype.render = function () {
const self = this;
return self.loadScript().then(function (AMap) {
self.create(AMap);
});
};
Object.assign(AMapSource, {
_checkExists: checkExists,
_getElementById: getElementById
});
module.exports = AMapSource;
|